import { ElMessage } from "element-plus"
import { Ref } from "vue"

interface RequestHookOpt<T> {
  execute?: boolean
  loading?: boolean
  defaultValue?: T
}

type QueryFnOpt = () => Promise<Resp<any>>

export function usePostRequest<T = any, U = any>(
  fn: QueryFnOpt,
  opt: RequestHookOpt<U>
) {
  const dataList = ref<T[]>([])

  const queryDataList = async () => {
    const { success, data } = await fn()
    if (success) {
      dataList.value = data
    }
  }
  if (opt.execute) {
    fn()
  }

  return {
    dataList,
    queryDataList,
  }
}
